package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.ReceiveCouponsApi;
import com.kgc.sbt.beans.CouponsType;
import com.kgc.sbt.beans.QueryCoupons;
import com.kgc.sbt.service.OrderService;
import com.kgc.sbt.utils.RequestResult;
import com.kgc.sbt.utils.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created on 2021/8/19.
 * <p>
 * Author: songhe
 * <p>
 * Description:
 */
@RestController
@Slf4j
@Api(tags = "4 获取优惠券")
@RequestMapping("/Coupon")
public class ReceiveCouponController {

    @Autowired
    private OrderService orderService;

    @Reference(timeout = 30000)
    private ReceiveCouponsApi receiveCouponsApi;

    /**
     * @return : java.lang.String
     * @author : songhe
     * @date : 2021/8/19
     * @description :  获取优惠卷
     */
    @ApiOperation(value = "查看优惠卷类型")
    @GetMapping("/queryCouponType")
    public RequestResult<List<CouponsType>> queryCouponType() {
        return ResultBuildUtil.success(receiveCouponsApi.queryCouponType());
    }

    /**
     * @return :
     * @author : songhe
     * @date : 2021/8/20
     * @description :  查询用户所有优惠卷
     */
    @ApiOperation(value = "查询用户所有优惠卷")
    @GetMapping("/queryUserCoupons")
    public RequestResult<List<QueryCoupons>> queryCouponType(HttpServletRequest request) {

        // 获取用户标识的token
        String token = request.getHeader("token");

        if (token == null) {
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }

        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if (userId == 0) {
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }

        return ResultBuildUtil.success(receiveCouponsApi.queryUserCoupons(userId));
    }

    /**
     * @return :
     * @author : songhe
     * @date : 2021/8/21
     * @description :  消耗积分获取优惠卷
     */
    @ApiOperation(value = "消耗积分获取优惠卷")
    @GetMapping("/getCoupons")
    @ApiImplicitParam(name = "couponsNum", value = "优惠卷编号")
    public RequestResult<String> getCoupons(HttpServletRequest request, String couponsNum) {


        // 获取用户标识的token
        String token = request.getHeader("token");

        if (token == null) {
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }

        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if (userId == 0) {
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }

        return ResultBuildUtil.success(receiveCouponsApi.getCoupons(userId, couponsNum));
    }

    /**
     * @return :
     * @author : songhe
     * @date : 2021/8/23
     * @description :  删除优惠卷
     */
    @ApiOperation(value = "删除优惠卷")
    @ApiIgnore
    @GetMapping("/deleteByCouponId")
    public String deleteBycouponId(Integer couponId) {
        return receiveCouponsApi.deleteBycouponId(couponId);
    }


}
